home *** CD-ROM | disk | FTP | other *** search
/ The Games Machine 80 / XENIATGM80.iso / Goodies / Blood 2 / Source / data.z / ModelObject.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-02  |  6.1 KB  |  266 lines

  1. // ----------------------------------------------------------------------- //
  2. //
  3. // MODULE  : ModelObject.cpp
  4. //
  5. // PURPOSE : ModelObject class - implementation
  6. //
  7. // CREATED : 12/31/97
  8. //
  9. // ----------------------------------------------------------------------- //
  10.  
  11. #include "ModelObject.h"
  12. #include "cpp_server_de.h"
  13. #include "ObjectUtilities.h"
  14. #include "SharedDefs.h"
  15.  
  16.  
  17. BEGIN_CLASS(CModelObject)
  18. END_CLASS_DEFAULT_FLAGS(CModelObject, BaseClass, NULL, NULL, CF_HIDDEN)
  19.  
  20.  
  21. // ----------------------------------------------------------------------- //
  22. //
  23. //    ROUTINE:    CModelObject::CModelObject
  24. //
  25. //    PURPOSE:    Initialize
  26. //
  27. // ----------------------------------------------------------------------- //
  28.  
  29. CModelObject::CModelObject() : BaseClass(OT_MODEL)
  30. {
  31.     m_bFirstUpdate = DTRUE;
  32.  
  33.     m_bRotate = DFALSE;
  34.     m_bStopRotateOnGround = DTRUE;
  35.     m_fXRotVel = 0.0;
  36.     m_fYRotVel = 0.0;
  37.     m_fZRotVel = 0.0;
  38.     m_fLastTime = 0.0f;
  39.     m_fPitch = 0.0f;
  40.     m_fYaw = 0.0f;
  41.     m_fRoll = 0.0f;
  42.  
  43.     VEC_INIT(m_vBaseDims);
  44. }
  45.  
  46.  
  47. // ----------------------------------------------------------------------- //
  48. //
  49. //    ROUTINE:    CModelObject::~CModelObject
  50. //
  51. //    PURPOSE:    Destructor
  52. //
  53. // ----------------------------------------------------------------------- //
  54.  
  55. CModelObject::~CModelObject()
  56. {
  57. }
  58.  
  59.  
  60. // ----------------------------------------------------------------------- //
  61. //
  62. //    ROUTINE:    CModelObject::EngineMessageFn
  63. //
  64. //    PURPOSE:    Handle engine messages
  65. //
  66. // ----------------------------------------------------------------------- //
  67.  
  68. DDWORD CModelObject::EngineMessageFn(DDWORD messageID, void *pData, DFLOAT lData)
  69. {
  70.     switch(messageID)
  71.     {
  72.         case MID_UPDATE:
  73.         {
  74.             if( !Update(( DVector * )pData))
  75.             {
  76.                 g_pServerDE->RemoveObject(m_hObject);
  77.             }
  78.             break;
  79.         }
  80.  
  81.         case MID_PRECREATE:
  82.         {
  83.             ObjectCreateStruct* pStruct = ( ObjectCreateStruct * )pData;
  84.             break;
  85.         }
  86.  
  87.         case MID_INITIALUPDATE:
  88.         {
  89.             InitialUpdate(( DVector * )pData );
  90.             break;
  91.         }
  92.  
  93.         default : break;
  94.     }
  95.  
  96.  
  97.     return BaseClass::EngineMessageFn(messageID, pData, lData);
  98. }
  99.  
  100.  
  101. // ----------------------------------------------------------------------- //
  102. //
  103. //    ROUTINE:    CModelObject::Setup
  104. //
  105. //    PURPOSE:    Set up a ModelObject with the information needed
  106. //
  107. // ----------------------------------------------------------------------- //
  108.  
  109. void CModelObject::Setup( DFLOAT fLifeTime, DVector *pvRotationPeriods,
  110.                          DBOOL bStopRotateOnGround, DBOOL bRandomizeRotation)
  111. {
  112.     DFLOAT fUpdateTime;
  113.  
  114.     // Set the life of the object...
  115.     // Numbers less than and equal to zero means it lasts forever...
  116.     m_fLifeTime = fLifeTime;
  117.  
  118.     if (bRandomizeRotation)
  119.     {
  120.         m_fPitch    = g_pServerDE->Random(0.0f, PIx2);
  121.         m_fYaw        = g_pServerDE->Random(0.0f, PIx2);
  122.         m_fRoll        = g_pServerDE->Random(0.0f, PIx2);
  123.     }
  124.  
  125.     if( pvRotationPeriods )
  126.     {
  127.         float mag;
  128.         mag = VEC_MAGSQR( *pvRotationPeriods );
  129.         if( mag > 0.001 )
  130.         {
  131.             m_bRotate = DTRUE;
  132.             m_bStopRotateOnGround = bStopRotateOnGround;
  133.             
  134.             if( pvRotationPeriods->x < -0.001 || 0.001f < pvRotationPeriods->x )
  135.                 m_fXRotVel = MATH_CIRCLE / pvRotationPeriods->x;
  136.             if( pvRotationPeriods->y < -0.001 || 0.001f < pvRotationPeriods->y )
  137.                 m_fYRotVel = MATH_CIRCLE / pvRotationPeriods->y;
  138.             if( pvRotationPeriods->z < -0.001 || 0.001f < pvRotationPeriods->z )
  139.                 m_fZRotVel = MATH_CIRCLE / pvRotationPeriods->z;
  140.  
  141.         }
  142.     }
  143.  
  144.     // Only update if we need to...
  145.     if( m_bRotate )
  146.         fUpdateTime = 0.01f;
  147.     else if( m_fLifeTime > 0.0f )
  148.         fUpdateTime = m_fLifeTime;
  149.     else
  150.         fUpdateTime = 0.0f;
  151.  
  152.     g_pServerDE->SetNextUpdate( m_hObject, fUpdateTime );
  153. }
  154.  
  155.  
  156.  
  157. // ----------------------------------------------------------------------- //
  158. //
  159. //    ROUTINE:    CModelObject::UpdateRotation
  160. //
  161. //    PURPOSE:    Update sprite scaling
  162. //
  163. // ----------------------------------------------------------------------- //
  164.  
  165. void CModelObject::UpdateRotation()
  166. {
  167.     DFLOAT fTime = g_pServerDE->GetTime();
  168.     DFLOAT fDeltaTime = fTime - m_fLastTime;
  169.  
  170.     DRotation rRot;
  171.     g_pServerDE->GetObjectRotation( m_hObject, &rRot );
  172.  
  173.     if( m_fXRotVel < 0.0f || 0.0f < m_fXRotVel )
  174.         m_fPitch += m_fXRotVel * fDeltaTime;
  175.     if( m_fYRotVel < 0.0f || 0.0f < m_fYRotVel )
  176.         m_fYaw += m_fYRotVel * fDeltaTime;
  177.     if( m_fZRotVel < 0.0f || 0.0f < m_fZRotVel )
  178.         m_fRoll += m_fZRotVel * fDeltaTime;
  179.  
  180.     g_pServerDE->SetupEuler(&rRot, m_fPitch, m_fYaw, m_fRoll);
  181.     g_pServerDE->SetObjectRotation(m_hObject, &rRot);    
  182.  
  183.     m_fLastTime = fTime;
  184. }
  185.  
  186.  
  187. // ----------------------------------------------------------------------- //
  188. //
  189. //    ROUTINE:    CModelObject::InitialUpdate
  190. //
  191. //    PURPOSE:    Do initial updating
  192. //
  193. // ----------------------------------------------------------------------- //
  194.  
  195. DBOOL CModelObject::InitialUpdate(DVector*)
  196. {
  197.     g_pServerDE->GetObjectDims(m_hObject, &m_vBaseDims);
  198.  
  199.     g_pServerDE->SetNextUpdate(m_hObject, (DFLOAT)0.01);
  200.  
  201.     return DTRUE;
  202. }
  203.  
  204.  
  205. // ----------------------------------------------------------------------- //
  206. //
  207. //    ROUTINE:    CModelObject::FirstUpdate
  208. //
  209. //    PURPOSE:    Do First updating
  210. //
  211. // ----------------------------------------------------------------------- //
  212.  
  213. void CModelObject::FirstUpdate()
  214. {
  215.     m_fStartTime = g_pServerDE->GetTime();
  216. }
  217.  
  218. // ----------------------------------------------------------------------- //
  219. //
  220. //    ROUTINE:    CModelObject::Update
  221. //
  222. //    PURPOSE:    Update the ModelObject
  223. //
  224. // ----------------------------------------------------------------------- //
  225.  
  226. DBOOL CModelObject::Update(DVector* pMovement)
  227. {
  228.     CollisionInfo collisionInfo;
  229.  
  230.     if( m_bFirstUpdate )
  231.     {
  232.         FirstUpdate();
  233.         m_bFirstUpdate = DFALSE;
  234.     }
  235.  
  236.     if( m_bRotate ) 
  237.     {
  238.         if( m_bStopRotateOnGround )
  239.         {
  240.             g_pServerDE->GetStandingOn( m_hObject, &collisionInfo );
  241.             
  242.             if( collisionInfo.m_hObject) 
  243.                 m_bRotate = DFALSE;
  244.             else
  245.                 UpdateRotation();
  246.         }
  247.         else
  248.             UpdateRotation();
  249.     }
  250.  
  251.     DFLOAT fTime = g_pServerDE->GetTime();
  252.  
  253.     // Only update if we need to...
  254.     if( m_bRotate )
  255.         g_pServerDE->SetNextUpdate( m_hObject, 0.01f );
  256.     else
  257.         g_pServerDE->SetNextUpdate( m_hObject, m_fLifeTime - ( fTime - m_fStartTime ));
  258.  
  259.     if( m_fLifeTime > 0.0f )
  260.         return (fTime < m_fStartTime + m_fLifeTime );
  261.     else
  262.         return DTRUE;
  263. }
  264.  
  265.  
  266.